﻿using System;
using System.Windows.Forms;

namespace SourceBackup
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string startupPath = string.Empty;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length > 0)
            {
                startupPath = args[0];
                if (startupPath == "-m") //application called itself to elevate to admin access
                {
                    if (args.Length < 3)
                    {
                        MessageBox.Show("This application must be executed with 2 parameters\r\n1. Full source file path\r\n2. Full destination file path",
                            "Command line parameters required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        string sourceFile = args[1];
                        string destinationFile = args[2];

                        try
                        {
                            if (System.IO.File.Exists(destinationFile))
                            {
                                System.IO.File.SetAttributes(destinationFile, System.IO.FileAttributes.Normal);
                                System.IO.File.Delete(destinationFile);
                            }

                            System.IO.File.Move(sourceFile, destinationFile);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    return;
                }                
            }

            if (Properties.Settings.Default.NewVersion)
            {
                Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.NewVersion = false;
                Properties.Settings.Default.Save();
            }
            if (Properties.Settings.Default.BackupFilters == null)
            {
                Properties.Settings.Default.BackupFilters = new System.Collections.Specialized.StringCollection();
                string tmpFilter = Properties.Resources.All_files;
                Properties.Settings.Default.BackupFilters.Add(tmpFilter);
                tmpFilter = Properties.Resources.Default_Filter;
                Properties.Settings.Default.BackupFilters.Add(tmpFilter);
            }

            Application.Run(new MainForm(startupPath));
        }
    }
}
